/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyectoprogra3;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import javax.swing.*;
/**
 * 
 * @author Cáceres Cristóbal
 */
public class Ventana extends JFrame implements ActionListener{

    private LinkedList lstPreguntas ;
    private JScrollPane scroll = new JScrollPane();
    private JPanel pnlTituloFormulario, pnlFinal,pnlPreguntas,pnlTotal;
    private JTextField txtTituloFormulario ;
    private JButton btnAgregar, btnGuardar;
    
    public Ventana() {
        
        this.pack();//permite que los componentes se dibujen con el tamaño preferencial de cada uno....
        this.setSize(800,600);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.dispose();
        
        this.pnlTituloFormulario = new JPanel();
        this.txtTituloFormulario = new JTextField(50);
        this.pnlTituloFormulario.add(txtTituloFormulario);
        
        this.pnlPreguntas = new JPanel();        
        this.pnlPreguntas.setLayout(new BoxLayout(pnlPreguntas,BoxLayout.Y_AXIS));   
        this.lstPreguntas = new LinkedList();
        
        this.pnlFinal = new JPanel();
        this.pnlFinal.setLayout(new FlowLayout());
        this.btnAgregar = new JButton("Agregar Nueva Pregunta");
        this.btnAgregar.addActionListener(this);
        this.btnGuardar = new JButton("Guardar Formulario");
        this.btnGuardar.addActionListener(this);
        this.pnlFinal.add(btnAgregar);
        this.pnlFinal.add(btnGuardar);
                
        
        this.pnlTotal = new JPanel();
        this.pnlTotal.setLayout(new BoxLayout(pnlTotal,BoxLayout.Y_AXIS));
        this.pnlTotal.add(pnlTituloFormulario);
        this.pnlTotal.add(pnlPreguntas);
        this.pnlTotal.add(pnlFinal);
        
        this.scroll.setSize(798, 598);
        this.scroll.setViewportView(pnlTotal);   
        this.add(scroll);
        Cargar();
    }

    
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(btnAgregar)){
            Pregunta h = new Pregunta();
            this.lstPreguntas.add(h);
            this.pnlPreguntas.add(h);    
            this.pnlPreguntas.revalidate();
            this.pnlPreguntas.repaint();
        }else if (e.getSource().equals(btnGuardar)){
            Guardar();
        }
    }
     public void Guardar() {
        try{
            FileOutputStream out = new FileOutputStream("resultados.txt");
            ObjectOutputStream obj = new ObjectOutputStream(out);
            obj.writeObject(lstPreguntas);
            obj.flush();
            JOptionPane.showMessageDialog(null, "Guardado exitosamente");
        }catch (Exception e){
            
        }
    }
    public void Cargar(){
        try{
            FileInputStream in = new FileInputStream("resultados.txt");
            ObjectInputStream obj = new ObjectInputStream(in);
            lstPreguntas = (LinkedList<Pregunta>)(obj.readObject());
            for(Object h : lstPreguntas){
                this.pnlPreguntas.add((Pregunta)h);    
            }
            this.pnlPreguntas.revalidate();
            this.pnlPreguntas.repaint();
           JOptionPane.showMessageDialog(null, "Cargado exitosamente");
        }catch(Exception e){
            
        }
    }
}

